// pages/homepage/homepage.js
const util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
		/* 动态数组 */
		dynamic:[],
		/* 没有数据显示 */
		noneData:false,
		/* 分页页码 */
		page:1,
		/* 列表margin */
		MarginTop:0,
		/* 列表高度 */
		Height:0,
		/* 机器人移动的位置 */
		leftLooks: 280,
    topLooks: 350,
		/* 轮播图片 */
		imgUrls:[],
		/* 轮播属性设置 */
		indicatorDots:false,
		autoplay:true,
		interval: 5000,
    duration: 1000,
		/* 是否下拉刷新 */
		refresh:false,
		/* 分类数据 */
		classifyList:[
			{
				src:"../../images/comprehensive-policy.png",
				name:"综合政策",
				url:"../comprehensivePolicy/comprehensivePolicy?type=11",
				type:11
			},
			{
				src:"../../images/policy-interpretation.png",
				name:"政策解读",
				url:"../comprehensivePolicy/comprehensivePolicy?type=13",
				type:13
			},
			{
				src:"../../images/classic-case.png",
				name:"经典案例",
				url:"../classicCase/classicCase",
				type:12
			},
			{
				src:"../../images/ericsson-insight.png",
				name:"立信洞察",
				url:"../comprehensivePolicy/comprehensivePolicy?type=14",
				type:14
			},
			{
				src:"../../images/tax-calculation.png",
				name:"个税计算",
				url:"../taxCalculation/taxCalculation",
				type:20
			}
		],
		/* 创建多个自定义组件 */
		pages:[1],
		/* 当前页面的page值 */
		pageLength:1,
		/* 排列方式 */
		orderBy:"publish_at",
		/* 最多页码 */
		maxPage:2,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
		wx.setStorageSync('collect',true);
		wx.setStorageSync('maxPage',1);
		wx.showShareMenu({  
			withShareTicket:true,
			menus: ['shareAppMessage', 'shareTimeline']
		})
		
  },
	
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
		/* 获取手机可视区域宽高 */
		wx.getSystemInfo({
      success: function(res) {
				wx.setStorageSync("phoneWidth",res.windowWidth);
				wx.setStorageSync("phoneHeight",res.windowHeight);
      },
    });
		/* 获取图片宽高 */
		wx.createSelectorQuery().selectAll('.robot-image').boundingClientRect(function (rect) {
			wx.setStorageSync("robotWidth",rect[0].width);
			wx.setStorageSync("robotHeight",rect[0].height);
		}).exec();
		var _this=this;
		wx.createSelectorQuery().selectAll('.homepage-fixed').boundingClientRect(function (rect) {
			_this.setData({
				MarginTop:rect[0].height,
				//nHeight:wx.getStorageSync("phoneHeight")-rect[0].height
			})
		}).exec();
		
		/* 获取轮播图 */
		this.queryWheelPlantingPhoto();
  },
	/* 轮播图 */
	queryWheelPlantingPhoto:function(){
		var _this=this;
		wx.request({
			url: util.url+"/system/queryWheelPlantingPhoto",
			data: {},
			method:"post",
			header: {
				'content-type': 'application/json' 
			},
			success(res) {
				if(res.data.resultCode==0){
					var articleList=res.data.resultData.articleList;
					_this.setData({
						imgUrls:articleList
					})
				}
				
			}
		})
	},
	/* 轮播图跳转 */
	jumpSwiper:function(e){
		wx.navigateTo({
			url:"../"+e.currentTarget.dataset.url
		})
	},
	/* 查询最新动态 */
	queryArticle:function(){
		var _this=this;
		if(wx.getStorageSync('maxPage')>this.data.pageLength){
			this.data.pageLength+=1;
			this.data.pages.push(this.data.pageLength);
			this.setData({
				pages:this.data.pages,
				noneData:false
			})
			/* 保存最新的最大页码数 */
			var timer=setInterval(function(){
				if(wx.getStorageSync('newPage')){
					_this.data.maxPage=wx.getStorageSync('maxPage');
					wx.setStorageSync('newPage',false);
					clearInterval(timer);
				}
			},500);
		}else{
			this.setData({
				noneData:true
			})
		}
		
	},
	
	/* 跳转到问题页面 */
	toQuestion:function(){
		wx.navigateTo({
			url:"../questions/questions"
		})
	},
	/* 滑动机器人 */
	imagetouchmove:function(e){
		var phoneWidth=wx.getStorageSync("phoneWidth");
		var phoneHeight=wx.getStorageSync("phoneHeight");
		var robotWidth=wx.getStorageSync("robotWidth");
		var robotHeight=wx.getStorageSync("robotHeight");
		e.touches[0].clientX=e.touches[0].clientX<0?0:e.touches[0].clientX;
		e.touches[0].clientY=e.touches[0].clientY<0?0:e.touches[0].clientY;
		e.touches[0].clientX=e.touches[0].clientX>phoneWidth-robotWidth?phoneWidth-robotWidth:e.touches[0].clientX;
		e.touches[0].clientY=e.touches[0].clientY>phoneHeight-robotHeight?phoneHeight-robotHeight:e.touches[0].clientY;
		
    this.setData({
      leftLooks: e.touches[0].clientX,
      topLooks: e.touches[0].clientY
    })
  },
	/* 跳转到搜索页面 */
	toSearch:function(){
		wx.navigateTo({
			url: "../searchPage/searchPage"
		})
	},
	/* 跳转到分类页面 */
	todetail:function(e){
		wx.navigateTo({
			url:e.currentTarget.dataset.url
		})
	},
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
		if(!this.data.noneData){
			wx.setStorageSync('newPage',false);
			if(this.data.maxPage>-1){
				wx.setStorageSync('maxPage',this.data.maxPage);
			}
			this.queryArticle();
		}
  },
	
	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {
		this.setData({
			pages:[],
			pageLength:0,
			refresh:true,
		})
		wx.setStorageSync('maxPage',1);
		this.queryArticle();
		this.queryWheelPlantingPhoto();
		wx.showNavigationBarLoading() //在标题栏中显示加载
	},
	/**
	 * 用户点击右上角分享朋友
	 */
	onShareAppMessage: function (e) {
		
	},
	/**
	* 用户点击右上角分享朋友圈
	*/
	onShareTimeline: function () {

	 },
})